package network2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer extends JFrame implements Runnable{
	
	ServerSocket server = null;
	Socket socket = null;
	boolean threadFlag = true;
	
	InputStream is = null;
	InputStreamReader isr = null;
	BufferedReader br = null;
	
	BufferedWriter bw = null;
	OutputStream os = null;
	OutputStreamWriter osw = null;

	
	StringBuilder data = new StringBuilder();
	
	private JPanel contentPane;
	private JPanel panel;
	private JButton btnStart;
	private JButton btnStop;
	private JTextField textField;
	private JScrollPane scrollPane;
	private JTextPane result;
	private JPanel panel_1;
	private JButton btnNewButton;
	private JTextField message;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EchoServer frame = new EchoServer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EchoServer() {
		setTitle("Echo SERVER");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 505, 544);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		
		try{
			InetAddress ia = InetAddress.getLocalHost();
			setTitle(ia.getHostAddress());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnStart(), BorderLayout.WEST);
			panel.add(getBtnStop(), BorderLayout.EAST);
			panel.add(getTextField(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JButton getBtnStart() {
		if (btnStart == null) {
			btnStart = new JButton("Server Start");
			btnStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						threadFlag = true;
						btnStart.setEnabled(false);
						btnStop.setEnabled(true);
						data.append("<font size='5' color='red'>서버가 시작됨</font><br/>");
						result.setText(data.toString());

						server = new ServerSocket(4444);
						Thread t = new Thread(EchoServer.this);
						t.start();
						
					}catch(Exception ex){
						
					}
					
				}
			});
		}
		return btnStart;
	}
	private JButton getBtnStop() {
		if (btnStop == null) {
			btnStop = new JButton("Server Stop");
			btnStop.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					threadFlag = false;
					btnStart.setEnabled(true);
					btnStop.setEnabled(false);
					data.append("<font size='5' color='#0000ff'>서버가 중지됨</font><br/><br/>");
					result.setText(data.toString());
					try{
						Socket temp = new Socket("127.0.0.1",4444);
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
		}
		return btnStop;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setColumns(10);
		}
		return textField;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}

	@Override
	public void run() {
		try{
			data.append("클라이언트 접속 대기중....<br/>");
			result.setText(data.toString());
			
			socket = server.accept();
			InetAddress ia = socket.getInetAddress();
			data.append("<b>[" + ia.getHostAddress() 
					+ "] 클라이언트가 접속되었습니다.</b><br/>");
			result.setText(data.toString());
			
			os = socket.getOutputStream();
			osw = new OutputStreamWriter(os);
			bw = new BufferedWriter(osw);

			
			
			is = socket.getInputStream();
			isr = new InputStreamReader(is);
			br = new BufferedReader(isr);

			data.append("데이터 수신 대기...<br/>");
			result.setText(data.toString());
			
			
			while(threadFlag){
			
				String str = br.readLine();
				data.append(str + "<br/>");
				
				result.setText(data.toString());
				
				
			}
			
			br.close();
			isr.close();
			is.close();
			
			bw.close();
			osw.close();
			os.close();
			
			server.close();

		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getBtnNewButton(), BorderLayout.EAST);
			panel_1.add(getMessage(), BorderLayout.CENTER);
		}
		return panel_1;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("전송");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						bw.write(message.getText()+ "<br/>");
						bw.newLine();
						bw.flush();
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getMessage() {
		if (message == null) {
			message = new JTextField();
			message.setColumns(10);
		}
		return message;
	}
}














